package com.taptech.doufu.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.services.personalcenter.AccountService;

import java.util.HashSet;
import java.util.Set;

public class SharedPreferenceUtil
{
    public static String NOTIFICATION_NUM = "notification_num";



    /**
     * 设置推送
     *
     * @param mContext
     * @return
     */
    public static SharedPreferences settingsPush(Context mContext)
    {
        SharedPreferences settingsPush = mContext.getSharedPreferences(Constant.Settings.PUSH, Context.MODE_PRIVATE);
        return settingsPush;
    }

    /**
     * 设置字体
     *
     * @param mContext
     * @return
     */
    public static SharedPreferences settings(Context mContext)
    {
        SharedPreferences settings = mContext.getSharedPreferences(Constant.Settings.NAME, Context.MODE_PRIVATE);
        return settings;
    }

    /**
     * 黑夜模式
     *
     * @param mContext
     * @return
     */
    public static SharedPreferences dayNightPreferences(Context mContext)
    {
        SharedPreferences dayNightPreferences = mContext.getSharedPreferences(Constant.DayNight.NAME, Context.MODE_PRIVATE);
        return dayNightPreferences;
    }

    /**
     * 省流量模式
     *
     * @param mContext
     * @return
     */
    public static SharedPreferences trafficPreferences(Context mContext)
    {
        SharedPreferences trafficPreferences = mContext.getSharedPreferences(Constant.Traffic.NAME, Context.MODE_PRIVATE);
        return trafficPreferences;
    }

    /**
     * 引导图
     *
     * @param mContext
     * @return
     */
    public static SharedPreferences mGuide(Context mContext)
    {
        SharedPreferences mGuide = mContext.getSharedPreferences(Constant.Guide.NAME, Context.MODE_PRIVATE);
        return mGuide;
    }

    /**
     * 小说创建
     */
    public static SharedPreferences createNovel(Context mContext)
    {
        SharedPreferences mGuide = mContext.getSharedPreferences(Constant.NovelCreate.NAME, Context.MODE_PRIVATE);
        return mGuide;
    }

    /**
     * 小说草稿
     */
    public static SharedPreferences novelDrafts(Context mContext)
    {
        SharedPreferences mGuide = mContext.getSharedPreferences(Constant.NovelDrafs.NAME, Context.MODE_PRIVATE);
        return mGuide;
    }

    /**
     * 小说发送
     */
    public static SharedPreferences novelSend(Context mContext)
    {
        SharedPreferences mGuide = mContext.getSharedPreferences(Constant.NovelSend.NAME, Context.MODE_PRIVATE);
        return mGuide;
    }

    public static Set<String> getLocalOrderData(Context mContext)
    {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences("user_order_data", Activity.MODE_PRIVATE);
        Set<String> userOrderList = sharedPreferences.getStringSet("user_order", new HashSet<String>());
     //   TTLog.i("TAG");
        return userOrderList;

    }

    public static void saveLocalOrderData(Context mContext)
    {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences("user_order_data", Activity.MODE_PRIVATE);
        Set<String> userOrderList = AccountService.getInstance().getUserReadOrders();
        SharedPreferences.Editor editor = sharedPreferences.edit().clear();
        editor.putStringSet("user_order", userOrderList);
        editor.commit();

    }


    public static boolean getRushGuideImage(Context mContext ){
        SharedPreferences sharedPreferences = mContext.getSharedPreferences("RushGuideImage", Activity.MODE_PRIVATE);
        boolean isClick = sharedPreferences.getBoolean("RushGuideImage", false);
        return isClick;
    }

    public static void saveRushGuideImage(Context mContext){
        SharedPreferences sharedPreferences = mContext.getSharedPreferences("RushGuideImage", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("RushGuideImage",true);
        editor.commit();
    }

    public static int getCartoonDisplayType(Context mContext , int type){
        SharedPreferences sharedPreferences = mContext.getSharedPreferences("cartoon_display", Activity.MODE_PRIVATE);
        int displayType = sharedPreferences.getInt("cartoon_display_type", type);
        return displayType;
    }

    public static void saveCartoonDisplayType(Context mContext , int type){
        SharedPreferences sharedPreferences = mContext.getSharedPreferences("cartoon_display", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("cartoon_display_type", type);
        editor.commit();
    }


    public static void saveProtocolDialog(Context context , String userId , String objectType){
        SharedPreferences sharedPreferences = context.getSharedPreferences("user_protocol_dialog" , Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        StringBuilder saveKey = new StringBuilder("protocol");
        saveKey.append(userId);
        saveKey.append(objectType);
        editor.putBoolean(saveKey.toString() , false );
        editor.commit();
    }

    public static boolean getProtocolDialog(Context context , String userId , String objectType){
        SharedPreferences sharedPreferences = context.getSharedPreferences("user_protocol_dialog" , Context.MODE_PRIVATE);
        StringBuilder saveKey = new StringBuilder("protocol");
        saveKey.append(userId);
        saveKey.append(objectType);
        boolean enableDialogShow = sharedPreferences.getBoolean(saveKey.toString() , true);
        return enableDialogShow;
    }


        public static String PREFERENCE_NAME = "SQJZCONFIG";

        /**
         * put string preferences
         *
         * @param context
         * @param key The name of the preference to modify
         * @param value The new value for the preference
         * @return True if the new values were successfully written to persistent storage.
         */
        public static boolean putString(Context context, String key, String value) {
            SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString(key, value);
            return editor.commit();
        }

        /**
         * get string preferences
         *
         * @param context
         * @param key The name of the preference to retrieve
         * @return The preference value if it exists, or null. Throws ClassCastException if there is a preference with this
         *         name that is not a string
         * @see #getString(Context, String, String)
         */
        public static String getString(Context context, String key) {
            return getString(context, key, null);
        }

        /**
         * get string preferences
         *
         * @param context
         * @param key The name of the preference to retrieve
         * @param defaultValue Value to return if this preference does not exist
         * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
         *         this name that is not a string
         */
        public static String getString(Context context, String key, String defaultValue) {
            SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
            return settings.getString(key, defaultValue);
        }

        /**
         * put int preferences
         *
         * @param context
         * @param key The name of the preference to modify
         * @param value The new value for the preference
         * @return True if the new values were successfully written to persistent storage.
         */
        public static boolean putInt(Context context, String key, int value) {
            SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putInt(key, value);
            return editor.commit();
        }

        /**
         * get int preferences
         *
         * @param context
         * @param key The name of the preference to retrieve
         * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
         *         name that is not a int
         * @see #getInt(Context, String, int)
         */
        public static int getInt(Context context, String key) {
            return getInt(context, key, 0);
        }

        /**
         * get int preferences
         *
         * @param context
         * @param key The name of the preference to retrieve
         * @param defaultValue Value to return if this preference does not exist
         * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
         *         this name that is not a int
         */
        public static int getInt(Context context, String key, int defaultValue) {
            SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
            return settings.getInt(key, defaultValue);
        }

        /**
         * put long preferences
         *
         * @param context
         * @param key The name of the preference to modify
         * @param value The new value for the preference
         * @return True if the new values were successfully written to persistent storage.
         */
        public static boolean putLong(Context context, String key, long value) {
            SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putLong(key, value);
            return editor.commit();
        }

        /**
         * get long preferences
         *
         * @param context
         * @param key The name of the preference to retrieve
         * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
         *         name that is not a long
         * @see #getLong(Context, String, long)
         */
        public static long getLong(Context context, String key) {
            return getLong(context, key, -1);
        }

        /**
         * get long preferences
         *
         * @param context
         * @param key The name of the preference to retrieve
         * @param defaultValue Value to return if this preference does not exist
         * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
         *         this name that is not a long
         */
        public static long getLong(Context context, String key, long defaultValue) {
            SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
            return settings.getLong(key, defaultValue);
        }

        /**
         * put float preferences
         *
         * @param context
         * @param key The name of the preference to modify
         * @param value The new value for the preference
         * @return True if the new values were successfully written to persistent storage.
         */
        public static boolean putFloat(Context context, String key, float value) {
            SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putFloat(key, value);
            return editor.commit();
        }

        /**
         * get float preferences
         *
         * @param context
         * @param key The name of the preference to retrieve
         * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
         *         name that is not a float
         * @see #getFloat(Context, String, float)
         */
        public static float getFloat(Context context, String key) {
            return getFloat(context, key, -1);
        }

        /**
         * get float preferences
         *
         * @param context
         * @param key The name of the preference to retrieve
         * @param defaultValue Value to return if this preference does not exist
         * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
         *         this name that is not a float
         */
        public static float getFloat(Context context, String key, float defaultValue) {
            SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
            return settings.getFloat(key, defaultValue);
        }

        /**
         * put boolean preferences
         *
         * @param context
         * @param key The name of the preference to modify
         * @param value The new value for the preference
         * @return True if the new values were successfully written to persistent storage.
         */
        public static boolean putBoolean(Context context, String key, boolean value) {
            SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean(key, value);
            return editor.commit();
        }

        /**
         * get boolean preferences, default is false
         *
         * @param context
         * @param key The name of the preference to retrieve
         * @return The preference value if it exists, or false. Throws ClassCastException if there is a preference with this
         *         name that is not a boolean
         * @see #getBoolean(Context, String, boolean)
         */
        public static boolean getBoolean(Context context, String key) {
            return getBoolean(context, key, false);
        }

        /**
         * get boolean preferences
         *
         * @param context
         * @param key The name of the preference to retrieve
         * @param defaultValue Value to return if this preference does not exist
         * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
         *         this name that is not a boolean
         */
        public static boolean getBoolean(Context context, String key, boolean defaultValue) {
            SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
            return settings.getBoolean(key, defaultValue);
        }
}
